package io.github.mcsdk.ui.components;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.network.chat.Component;

import java.util.function.Consumer;

/**
 * Search box component with customizable styling and callbacks
 *
 * <p>Features:</p>
 * <ul>
 *   <li>Real-time text change notifications</li>
 *   <li>Placeholder text support</li>
 *   <li>Custom validation</li>
 *   <li>Styling options</li>
 * </ul>
 *
 * @author MC SDK Team
 * @version 1.0.0
 * @since 1.0.0
 */
public class SearchBox extends EditBox {

    private final Consumer<String> onTextChanged;
    private String lastValue = "";

    /**
     * Creates a new search box
     *
     * @param x X position
     * @param y Y position
     * @param width Width
     * @param height Height
     * @param onTextChanged Callback when text changes
     */
    public SearchBox(int x, int y, int width, int height, Consumer<String> onTextChanged) {
        super(Minecraft.getInstance().font, x, y, width, height, Component.literal("Search"));
        this.onTextChanged = onTextChanged;
        this.setMaxLength(256);
        this.setBordered(true);
        this.setHint(Component.literal("Search..."));
    }

    /**
     * Creates a new search box with custom placeholder
     *
     * @param x X position
     * @param y Y position
     * @param width Width
     * @param height Height
     * @param placeholder Placeholder text
     * @param onTextChanged Callback when text changes
     */
    public SearchBox(int x, int y, int width, int height, String placeholder, Consumer<String> onTextChanged) {
        super(Minecraft.getInstance().font, x, y, width, height, Component.literal("Search"));
        this.onTextChanged = onTextChanged;
        this.setMaxLength(256);
        this.setBordered(true);
        this.setHint(Component.literal(placeholder));
    }

    /**
     * Creates a new search box with font
     *
     * @param font Font to use
     * @param x X position
     * @param y Y position
     * @param width Width
     * @param height Height
     * @param placeholder Placeholder text
     * @param onTextChanged Callback when text changes
     */
    public SearchBox(Font font, int x, int y, int width, int height, String placeholder, Consumer<String> onTextChanged) {
        super(font, x, y, width, height, Component.literal("Search"));
        this.onTextChanged = onTextChanged;
        this.setMaxLength(256);
        this.setBordered(true);
        this.setHint(Component.literal(placeholder));
    }

    @Override
    public void tick() {
        super.tick();

        // Check if value changed
        String currentValue = getValue();
        if (!currentValue.equals(lastValue)) {
            lastValue = currentValue;
            if (onTextChanged != null) {
                onTextChanged.accept(currentValue);
            }
        }
    }

    /**
     * Clears the search box
     */
    public void clear() {
        setValue("");
    }

    /**
     * Gets the current search text
     *
     * @return Search text
     */
    public String getSearchText() {
        return getValue();
    }

    /**
     * Sets the search text
     *
     * @param text Text to set
     */
    public void setSearchText(String text) {
        setValue(text);
    }

    /**
     * Checks if search box is empty
     *
     * @return true if empty
     */
    public boolean isEmpty() {
        return getValue().isEmpty();
    }
}